

#include "encoder.h"




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void eqep_gpio_init(void)
{
	// motor2  timer1 ch0 ch1
    rcu_periph_clock_enable(RCU_GPIOA);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
	
	// motor1  timer3 ch0 ch1
	rcu_periph_clock_enable(RCU_GPIOD);
	gpio_pin_remap_config(GPIO_TIMER3_REMAP, ENABLE);
    gpio_init(GPIOD, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
    gpio_init(GPIOD, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
	
    rcu_periph_clock_enable(RCU_GPIOG);
	
	#if 0
    gpio_init(GPIOG, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
    gpio_init(GPIOG, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_14);
	#endif

}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void eqep_resgiter_init(void)
{
	uint16_t i = 0;
	uint32_t Timerx_num[2] = {TIMER1, TIMER3};

	timer_ic_parameter_struct timer_icinitpara;
    timer_parameter_struct timer_initpara;

    rcu_periph_clock_enable(RCU_TIMER1);
	rcu_periph_clock_enable(RCU_TIMER3);

	timer_deinit(TIMER1);
	timer_deinit(TIMER3);

	for(i=0; i<2; i++)
	{
		timer_initpara.prescaler         = 0;
		timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
		timer_initpara.counterdirection  = TIMER_COUNTER_UP;
		timer_initpara.period            = MAGNET_ENCODER_LINE_NUM-1;
		timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
		timer_initpara.repetitioncounter = 0;
		timer_init(Timerx_num[i],&timer_initpara);
		
		/* TIMERx CH0 input capture configuration */
		timer_icinitpara.icpolarity  = TIMER_IC_POLARITY_RISING;
		timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
		timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
		timer_icinitpara.icfilter    = 0x04;
		timer_input_capture_config(Timerx_num[i],TIMER_CH_0,&timer_icinitpara);
		timer_input_capture_config(Timerx_num[i],TIMER_CH_1,&timer_icinitpara);
		
		
		timer_quadrature_decoder_mode_config(Timerx_num[i],TIMER_ENCODER_MODE2,TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_RISING);

		timer_auto_reload_shadow_enable(Timerx_num[i]);
		timer_enable(Timerx_num[i]);
	}
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void eqep_init(void)
{
	eqep_gpio_init();
	eqep_resgiter_init();
}



/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void speed_cal_eqep(STR_USERHDL *hdl, HAL_MotorNum_e num)
{


}


